{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1bcfef8-ab39-4b10-b19d-2dcdf12e9334",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918bc71f-e908-46c5-94dd-d7d6246d3363",
   "metadata": {},
   "outputs": [],
   "source": [
    "! cd modelscope-agent && pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad42f062-5664-44f8-82b7-6995a4f512a0",
   "metadata": {},
   "source": [
    "### 读取环境变量文件和工具config配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f06ad1-34c5-4032-9254-aab88d7fe19d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from modelscope.utils.config import Config\n",
    "\n",
    "# 配置环境变量，里面需要配置modelscope token来remote调用AI模型\n",
    "os.environ['TOOL_CONFIG_FILE'] ='../config/cfg_tool_template.json'\n",
    "os.environ['MODEL_CONFIG_FILE'] ='../config/cfg_model_template.json'\n",
    "\n",
    "os.environ['OUTPUT_FILE_DIRECTORY'] = './tmp'\n",
    "\n",
    "# ModelScope Token从这里获取: https://modelscope.cn/my/myaccesstoken\n",
    "print('请输入ModelScope Token，可以从这里获取: https://modelscope.cn/my/myaccesstoken')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()\n",
    "\n",
    "print('请输入OPENAI_API_KEY')\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = input()\n",
    "\n",
    "\n",
    "# 读取工具调用CONFIG文件，包括工具的名称和远程请求url\n",
    "tool_cfg_file = os.getenv('TOOL_CONFIG_FILE')\n",
    "print(tool_cfg_file)\n",
    "tool_cfg = Config.from_file(tool_cfg_file)\n",
    "\n",
    "model_cfg_file = os.getenv('MODEL_CONFIG_FILE')\n",
    "print(model_cfg_file)\n",
    "model_cfg = Config.from_file(model_cfg_file)\n",
    "model_name = 'openai'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51b195aa-8a5d-4571-8a58-ccaec0ca6e1a",
   "metadata": {},
   "source": [
    "### 构建langchian tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81a0805a-53d9-43e0-a63d-b9dd801039fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from modelscope_agent.agent import AgentExecutor\n",
    "from modelscope_agent.tools import LangchainTool\n",
    "from langchain.tools import ShellTool\n",
    "\n",
    "\n",
    "shell_tool = LangchainTool(ShellTool())\n",
    "\n",
    "\n",
    "additional_tool_list = {\n",
    "    shell_tool.name: shell_tool\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59b56b4a-8019-4f17-9801-861c8b55371d",
   "metadata": {},
   "source": [
    "### Agent构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faa84ccf-a437-4a9c-b647-0f46cbd80986",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.prompt import PromptGenerator\n",
    "from modelscope_agent.llm import LLMFactory\n",
    "CHATGPT_STSTEM_TEMPLATE = \"\"\"<|system|>:: You are an assistant tries to help human solve problems with tools.\"\"\"\n",
    "CHATGPT_INSTRUCTION_TEMPLATE = \"\"\"The following lists the tools available for the current session. \n",
    "The job of you to come up with a series of simple commands that will perform the task the human wants to perform. \n",
    "If you need to call the plug-in, you should give the commands in following format: \n",
    "\\'<|startofthink|>```JSON{\\\"api_name\\\": {api_name},\\\n",
    " \\\"parameters\\\": {\\\"parameter1\\\": \\\"value1\\\", \\\"parameter2\\\": \\\"value2\\\"}}```<|endofthink|>\\'. \n",
    "After calling plug-in, you need to generate a reasonable sumarization based on the execution result. \n",
    "If you think there is no need to call the plug-in, you can directly give the corresponding reply. \\n\\n<tool_list>\"\"\"\n",
    "\n",
    "\n",
    "CHATGPT_USER_TEMPLATE = \"\"\"<|user|>:<user_input>\"\"\"\n",
    "\n",
    "CHATGPT_EXEC_TEMPLATE = \"\"\"<|startofexec|><exec_result><|endofexec|>\\n\"\"\"\n",
    "\n",
    "CHATGPT_ASSISTANT_TEMPLATE = \"\"\"<|assistant|>:\"\"\"\n",
    "\n",
    "prompt_generator = PromptGenerator(CHATGPT_STSTEM_TEMPLATE, CHATGPT_INSTRUCTION_TEMPLATE, CHATGPT_USER_TEMPLATE, CHATGPT_EXEC_TEMPLATE, CHATGPT_ASSISTANT_TEMPLATE)\n",
    "\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)\n",
    "agent = AgentExecutor(llm, tool_cfg, additional_tool_list=additional_tool_list, prompt_generator=prompt_generator)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e8a8324-e589-47a1-b948-d2a89a75da53",
   "metadata": {
    "tags": []
   },
   "source": [
    "## tool检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c680dad9-3226-4972-9bd1-4b25a9583a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.reset()\n",
    "agent.run('生成一张鲜花的图片')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58b1a8d5-8bcc-4f04-b242-2b2a314320b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.reset()\n",
    "agent.run('please execuute command ls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a84e1bb-e643-4fcc-a416-0f8088dc6abc",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.reset()\n",
    "\n",
    "# remote=True为调用modelscope api，该服务免费支持QPS较低，建议部署在本地，将remote=False\n",
    "agent.run('使用地址识别模型，从下面的地址中找到省市区等元素，地址：浙江杭州市江干区九堡镇三村村一区')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dec5ff22-b22b-444a-a299-6e0264ed83d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
